//------------------------------------------------------------------------------
// <copyright file="ListBoxProperties.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.ComponentModel;
using System.Windows.Controls;
using System.Diagnostics;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows.Media;

namespace DotBeer.UI.WPFClient
{
    public class SelectorProperties
    {
        #region private members

        ScrollViewer listViewScrollViewer;

        #endregion

        #region properties

        public static readonly DependencyProperty ScrollToFirstItemOnItemsCollectionChangedProperty =
            DependencyProperty.RegisterAttached("ScrollToFirstItemOnItemsCollectionChanged",
                                                typeof(bool),
                                                typeof(Selector),
                                                new PropertyMetadata(new PropertyChangedCallback(ScrollToFirstItemOnItemsCollectionChanged_Changed)));

        #endregion

        #region constructors

        public SelectorProperties(Selector Selector)
        {
            Border border = VisualTreeHelper.GetChild(Selector, 0) as Border;
            this.listViewScrollViewer = border.Child as ScrollViewer;
            ((INotifyCollectionChanged)Selector.Items).CollectionChanged += new NotifyCollectionChangedEventHandler(SelectorProperties_CollectionChanged);
        }

        #endregion

        #region public methods

        public static void SetScrollToFirstItemOnItemsCollectionChanged(DependencyObject d, bool scroll)
        {
            d.SetValue(ScrollToFirstItemOnItemsCollectionChangedProperty, scroll);
        }

        public static bool GetScrollToFirstItemOnItemsCollectionChanged(DependencyObject d)
        {
            return (bool)d.GetValue(ScrollToFirstItemOnItemsCollectionChangedProperty);
        }

        #endregion

        #region private methods

        private static void ScrollToFirstItemOnItemsCollectionChanged_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                Selector Selector = d as Selector;
                Selector.Loaded += new RoutedEventHandler(selector_Loaded);
            }
        }

        private static void selector_Loaded(object sender, RoutedEventArgs e)
        {
            SelectorProperties SelectorProperties = new SelectorProperties(sender as Selector);
        }

        private void SelectorProperties_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Reset)
                this.listViewScrollViewer.ScrollToVerticalOffset(0);
        }

        #endregion
    }
}
